package com.yishun.springboot.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yishun.springboot.mapper.PermissionMapper;
import com.yishun.springboot.mapper.RoleMapper;
import com.yishun.springboot.mapper.TbMenuMapper;
import com.yishun.springboot.mapper.TbPageMapper;
import com.yishun.springboot.mapper.UserMapper;
import com.yishun.springboot.pojo.TbPage;
import com.yishun.springboot.pojo.User;
import com.yishun.springboot.service.TbPageService;
import com.yishun.springboot.util.ResultUtils;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class TbPageServiceImpl implements TbPageService {

	@Autowired
	TbPageMapper tbPageMapper;

	@Autowired
	UserMapper userDao;

	@Autowired
	TbMenuMapper tbMenuMapper;

	@Autowired
	RoleMapper roleDao;

	@Autowired
	PermissionMapper permissionMapper;

	public JSONObject getRoleAndPage(JSONObject jsonObject) {
		String roleId = jsonObject.getString("roleId");

		String userName = jsonObject.getString("userName");
		// Subject currentUser = SecurityUtils.getSubject();
		// User user = (User) currentUser.getSession().getAttribute("user");

		List<Map<String, Object>> li = userDao.getRoleAndPage(null, null);
		List<Map<String, Object>> lis = userDao.findByRoleIdAndUserName(null, null);
		JSONArray list = null;
		JSONArray lists = new JSONArray();
		JSONArray jsonArr = null;
		for (int i = 0; i <= li.size() - 1; i++) {
			Map<String, Object> map = li.get(i);
			list = new JSONArray();

			for (int y = 0; y <= li.size() - 1; y++) {
				Map<String, Object> mapVa = li.get(y);
				if ((Integer) map.get("page_id") == (Integer) mapVa.get("pagerank")) {
					jsonArr = new JSONArray();
					for (int f = 0; f <= lis.size() - 1; f++) {
						Map<String, Object> mapVal = lis.get(f);
						if ((Integer) mapVal.get("page_id") == (Integer) mapVa.get("page_id")) {
							jsonArr.add(mapVal);
						}
					}
					mapVa.put("menu", jsonArr);
					list.add(mapVa);
				}
			}
			map.put("data", list);
			if ((Integer) map.get("pagerank") == 0) {
				lists.add(map);
			}
		}
		Set<Object> stringSet = new HashSet<>();
		stringSet.add(roleId);
		List<Object> liststring = new ArrayList<Object>(stringSet);
		List<Map<String, Object>> liz = userDao.getRoleAndPage(roleId, userName);
		List<Map<String, Object>> lisz = userDao.findByRoleIdAndUserName(liststring, userName);
		JSONArray listsz = new JSONArray();
		for (int i = 0; i <= liz.size() - 1; i++) {
			Map<String, Object> map = liz.get(i);
			list = new JSONArray();

			for (int y = 0; y <= liz.size() - 1; y++) {
				Map<String, Object> mapVa = liz.get(y);
				if ((Integer) map.get("page_id") == (Integer) mapVa.get("pagerank")) {
					jsonArr = new JSONArray();
					for (int f = 0; f <= lisz.size() - 1; f++) {
						Map<String, Object> mapVal = lisz.get(f);
						if ((Integer) mapVal.get("page_id") == (Integer) mapVa.get("page_id")) {
							jsonArr.add(mapVal);
						}
					}
					mapVa.put("menu", jsonArr);
					list.add(mapVa);
				}
			}
			map.put("data", list);
			if ((Integer) map.get("pagerank") == 0) {
				listsz.add(map);
			}
		}
		JSONObject object = new JSONObject();
		object.put("thisJurisdiction", listsz);
		object.put("allJurisdiction", lists);

		return ResultUtils.jsonSuccess("查询成功", object);
	}

	@Override
	public JSONObject addMultiPage(List<Map<String, Object>> pageList, List<Map<String, Object>> menuList,
			String roleIdOrUsername) {
		// 获取当前用户的 Subject 对象
		Subject currentUser = SecurityUtils.getSubject();

		// 获取保存在 Session 中的用户信息
		User user = (User) currentUser.getSession().getAttribute("user");
		tbPageMapper.delTbPageRole(roleIdOrUsername);// 删除所有当前账号的权限
		Integer num = tbPageMapper.addMultiPage(pageList, roleIdOrUsername);// 添加新增的页面权限
		log.info(user.getUsername() + "添加页面权限");
		roleDao.delTbUserMenu(roleIdOrUsername);
		permissionMapper.delTbRolesPermission(roleIdOrUsername);
		num += tbMenuMapper.addMenu(menuList, roleIdOrUsername);

		log.info(user.getUsername() + "添加按钮权限");
		List<Map<String, Object>> list = new ArrayList<>();
		for (Map<String, Object> map : menuList) {
			Integer permissionId = tbMenuMapper.getTbMenuPermission(Integer.valueOf(map.get("menuId").toString()));
			if (permissionId != null) {
				Map<String, Object> maps = new HashMap<String, Object>();
				maps.put("ridOrUserName", roleIdOrUsername);
				maps.put("pid", permissionId);
				list.add(maps);
			}
		}
		permissionMapper.addTbRolesPermission(list, roleIdOrUsername);
		if (num >= 1) {
			return ResultUtils.jsonSuccessNotData("修改成功");
		} else {
			return ResultUtils.jsonFail("修改失败，请联系管理员");
		}
	}

	@Override
	public JSONObject upMultiPage(String roleId, List<Map<String, Object>> list) {
		tbPageMapper.delTbPageRole(roleId);
		Integer num = tbPageMapper.addMultiPage(list, roleId);
		for (Map<String, Object> map : list) {
			Integer permissionId = tbMenuMapper.getTbMenuPermission(Integer.valueOf(map.get("menuId").toString()));
			if (permissionId != null) {
				Map<String, Object> maps = new HashMap<String, Object>();
				maps.put("ridOrUserName", map.get("userNamerOrRoleId"));
				maps.put("pid", permissionId);
				list.add(maps);
			}
		}
		permissionMapper.addTbRolesPermission(list, roleId);
		if (num >= 1) {
			return ResultUtils.jsonSuccessNotData("修改成功");
		} else {
			return ResultUtils.jsonFail("修改失败，请联系管理员");
		}
	}

	@Override
	public JSONObject addTbPage(JSONObject jsonObject) {
		Integer pagerank = jsonObject.getInteger("pagerank");
		String pageName = jsonObject.getString("pageName");
		String pageLocation = jsonObject.getString("pageLocation");
		if (pagerank == 0 || pageName == null || pageName.equals("") || pageLocation == null
				|| pageLocation.equals("")) {
			return ResultUtils.jsonFail(400);
		}
		Integer num = tbPageMapper.addTbPage(pagerank, pageName, pageLocation);
		if (num >= 1) {
			return ResultUtils.jsonSuccessNotData("新增成功");
		} else {
			return ResultUtils.jsonFail("新增失败，请联系管理员");
		}
	}

	public JSONObject upTbPage(JSONObject jsonObject) {
		Integer pageId = jsonObject.getInteger("pageId");
		Integer pagerank = jsonObject.getInteger("pagerank");
		String pageName = jsonObject.getString("pageName");
		String pageLocation = jsonObject.getString("pageLocation");
		TbPage tbPage = new TbPage();
		tbPage.setPage_id(pageId);
		tbPage.setPage_location(pageLocation);
		tbPage.setPage_name(pageName);
		tbPage.setPagerank(pagerank);
		Integer num = tbPageMapper.upTbPage(tbPage);
		if (num >= 1) {
			return ResultUtils.jsonSuccessNotData("修改成功");
		} else {
			return ResultUtils.jsonFail("修改失败，请联系管理员");
		}
	}

	@Override
	public JSONObject delTbPage(Integer pageId) {
		if (pageId == 0) {
			return ResultUtils.jsonFail(400);
		}
		Integer num = tbPageMapper.delTbPage(pageId);
		if (num >= 1) {
			return ResultUtils.jsonSuccessNotData("删除成功");
		} else {
			return ResultUtils.jsonFail("删除失败，请联系管理员");
		}
	}
}
